import { _decorator, Component, Node, Prefab } from 'cc';
import { BoxItemUI } from './BoxItemUI';
import { PoolManager } from 'db://assets/Scripts/Framework/Managers/PoolManager';
import { PlayerController } from '../PlayerController';
const { ccclass, property } = _decorator;

@ccclass('BoxPanelUI')
export class BoxPanelUI extends Component {

    @property(Node)
    content: Node;

    @property(Prefab)
    boxItemPrefab: Prefab;

    start() {

    }

    update(deltaTime: number) {

    }

    Init() {
        let box = PlayerController.Instance.backPack.ContainBox;
        for (let i = 0; i < box.items.length; i++) {
            const element = box.items[i];
            let boxItem = this.content.children[i];
            if (!boxItem) boxItem = PoolManager.GetNodeByPrefab(this.boxItemPrefab, this.content);
            boxItem.getComponent(BoxItemUI).Init(element);
        }
        for (let i = box.items.length; i < this.content.children.length; i++) {
            const element = this.content.children[i];
            PoolManager.PutNode(element);
        }
        let suggests = this.content.children.filter((value, index, array) => { if (value.getComponent(BoxItemUI).IsSuggest) return value; });
        let unSuggests = this.content.children.filter((value, index, array) => { if (!value.getComponent(BoxItemUI).IsSuggest) return value; });
        suggests = suggests.concat(unSuggests);
        this.content.children.forEach(element => {
            element.setSiblingIndex(suggests.indexOf(element));
        });
    }

}


